using System;
using System.Collections.Generic;
using System.Text;

using System.Net.Sockets;
using System.Net;

using System.Windows.Forms;




namespace daxClass
{
    //EX CLASSE DXTCPCLIENT ABOLITA GRAN CAGATA...
    class dxTCPClient
    {
        #region delegate and event

        public delegate void OnReceivedDataEvent(Socket iRemote, string data);

        public event OnReceivedDataEvent _OnReceivedDataEvent;

        #endregion


        #region var






        const int SIZE_BUFFER_IN = 1024;

        Socket iClient;

        private string mAddress_server;
        private int mPort;

        private bool isConnectedToServer = false;

        private byte[] dataIn = new byte[SIZE_BUFFER_IN];

        

        #endregion

        public void Initialize_socket(string server_address,int port)
        {
            try
            {
                iClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                IPEndPoint iep = new IPEndPoint(IPAddress.Parse(server_address), port);

                mAddress_server = server_address;
                mPort = port;

                iClient.BeginConnect(iep, new AsyncCallback(OnConnected), iClient);

            }
            catch (SocketException err)
            {

                throw err;
            }

        }

        private void OnConnected(IAsyncResult iar)
        {
            try
            {
                Socket remote = (Socket)iar.AsyncState;
                remote.EndConnect(iar);

                isConnectedToServer = true;

                //inizio la ricezione
                remote.BeginReceive(dataIn, 0, dataIn.Length, SocketFlags.None, new AsyncCallback(OnReceivedData), remote);


                

            }
            catch (Exception err)
            {
                
               throw err;
            }
            
        }

        public void Close()
        {
            iClient.Close();
        }

        private void OnReceivedData(IAsyncResult iar)
        {
            try
            {
                Socket remote = (Socket)iar.AsyncState;



                
                
                int recv = remote.EndReceive(iar);



                string datiRicevuti = Encoding.UTF8.GetString(dataIn, 0, recv);

                if (recv == 0)
                {
                    //disconnect
                }
                else
                {
                    if (_OnReceivedDataEvent != null)
                    {
                        _OnReceivedDataEvent(remote, datiRicevuti);
                    }

                    remote.BeginReceive(dataIn, 0,dataIn.Length, SocketFlags.None, new AsyncCallback(OnReceivedData), remote);
                }

            }
            catch (SocketException err)
            {
                throw err;
            }
            catch (Exception err)
            {
                throw err;
            }
        }

        public void SendData(string data)
        {
            try
            {
                byte[] dataOut = new byte[SIZE_BUFFER_IN];
                dataOut = Encoding.UTF8.GetBytes(data);

                iClient.BeginSend(dataOut, 0, dataOut.Length, SocketFlags.None, new AsyncCallback(OnSentData), iClient);
            }
            catch (Exception err)
            {

                throw err;
            }
        }

        private void OnSentData(IAsyncResult iar)
        {
            Socket remote = (Socket)iar.AsyncState;
            int sent = remote.EndSend(iar);

            
        }

    }



  
}
